/*
 * Created on Feb 11, 2008
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.phalanx.client.properties;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author sday
 * This class stores all integral customizable options used by the Phalanx 
 * Server. This class is meant to be as generic as possible and allows the 
 * user to call up Preferences by referencing String getProperty() as well as
 * Integer getPropertyAsInt() and it's Double variant.
 */

public class ClientPreferences {
	Properties serverPrefStore;
	
	public ClientPreferences(){
		serverPrefStore = new Properties();
	}
	
	public ClientPreferences(Properties newProperties){
		serverPrefStore = newProperties;
	}
	
	public void setProperties(String setKey, String setValue){
		serverPrefStore.setProperty(setKey.toLowerCase(), setValue);
	}
	
	public void setProperties(String setKey, Integer setValue){
		String setValueString = setValue.toString();
		serverPrefStore.setProperty(setKey.toLowerCase(), setValueString);
	}
	
	public void setProperties(String setKey, Double setValue){
		String setValueString = setValue.toString();
		serverPrefStore.setProperty(setKey.toLowerCase(), setValueString);
	}
	
	public void setProperties(String setKey, Boolean setValue){
		String setValueString = setValue.booleanValue() == true ? "yes" : "no";
		serverPrefStore.setProperty(setKey.toLowerCase(), setValueString);
	}
	
	public String getProperties(String getKey){
		return serverPrefStore.getProperty(getKey.toLowerCase(), null);
	}
	
	public String getProperties(String getKey, String fallBackValue){
		return serverPrefStore.getProperty(getKey.toLowerCase(), fallBackValue);
	}
	
	public Integer getPropertiesAsInt(String getKey){
		Integer valueAsInt = null;
		try {
			String valueAsString = getProperties(getKey);
			
			if (valueAsString != null){
				valueAsInt = Integer.valueOf(valueAsString);
			}
		
		} catch (NumberFormatException nfe){
			return valueAsInt;
		}		
		return valueAsInt;
	}
	
	public Integer getPropertiesAsInt(String getKey, Integer fallBackValue){
		Integer valueAsInt = getPropertiesAsInt(getKey);
		
		if (valueAsInt == null) valueAsInt = fallBackValue;
		
		return valueAsInt;
	}
	
	public Double getPropertiesAsDbl(String getKey){
		Double valueAsDbl = null;
		try {
			String valueAsString = getProperties(getKey);
			
			if (valueAsString != null){
				valueAsDbl = Double.valueOf(valueAsString);
			}
		
		} catch (NumberFormatException nfe){
			return valueAsDbl;
		}		
		return valueAsDbl;
	}
	
	public Double getPropertiesAsDbl(String getKey, Double fallBackValue){
		Double valueAsDbl = getPropertiesAsDbl(getKey);
		
		if (valueAsDbl == null) valueAsDbl = fallBackValue;
		
		return valueAsDbl;
	}
	
	public Boolean getPropertiesAsBoolean(String getKey){
		String yesOrNo = getProperties(getKey).toLowerCase();
		Boolean boolValue = null;
		
		if (yesOrNo.equals("no")) boolValue = new Boolean(false);
		else {
			boolValue = new Boolean (yesOrNo.equals("yes") ? true : null);
		}
		
		return boolValue;
	}
	
	public Boolean getPropertiesAsBoolean(String getKey, Boolean fallBackValue){
		Boolean boolValue = getPropertiesAsBoolean(getKey);
		
		return boolValue == null ? fallBackValue : boolValue;
	}
	
	public void clearPrefs(){
		serverPrefStore.clear();
	}
	
	public ArrayList<String> getKeys (){
		ArrayList<String> arrayOfKeys = new ArrayList<String>();
		Enumeration<Object> keysEnum = serverPrefStore.keys();
		
		while(keysEnum.hasMoreElements()){
			String currentKey = keysEnum.nextElement().toString();
			arrayOfKeys.add(currentKey);
		}
		
		return arrayOfKeys;
	}
}
